import Vue from 'vue'
import VueRouter from 'vue-router'
import registerView from '../views/Register.vue'
import store from "../store/index";
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
import Cookies from 'js-cookie'

const routes = [
  {
    path: '/register',
    name: 'register',
    component: registerView,
    meta: {
      title: '注册页',
      adminOnly:false,
      needtoken:false
    }
  },{
    path: '/login',
    name: 'login',
    component: ()=>import("@/views/Login.vue"),
    meta: {
      title: '登录页',
      adminOnly:false,
      needtoken:false
    }
  },
  {
    path: '/',
    name: 'Index',
    component: () => import( '../views/Index.vue'),
    redirect:"/myArticle",
    meta: {
      title: '首页',
      adminOnly:false,
      needtoken:true
    },
    children:[
      {
        path: '/artcate',
        name: 'artcate',
        component: () => import( '../components/Artcate.vue'),
        meta: {
          title: '文章分类',
          adminOnly:true,
          needtoken:true,
          isNav:true
        },
      }
      ,{
        path: '/readArticle',
        name: 'Article',
        component: () => import( '../components/ReadArticle.vue'),
        meta: {
          title: '阅读文章',
          adminOnly:false,
          needtoken:true,
          isNav:false
        },
      },{
        path: '/addArticle',
        name: 'addArticle',
        component: () => import( '../components/addArticle.vue'),
        meta: {
          title: '发布文章',
          adminOnly:false,
          needtoken:true,
          isNav:true
        },
      },{
        path: '/usermanage',
        name: 'userManage',
        component: () => import( '../components/userManage.vue'),
        meta: {
          title: '用户列表',
          adminOnly:true,
          needtoken:true,
          isNav:true
        },
      }
      ,{
        path: '/myArticle',
        name: 'myArticle',
        component: () => import( '../components/myArticle.vue'),
        meta: {
          title: '我的文章',
          adminOnly:false,
          needtoken:true,
          isNav:true
        },
      }
    ]
  }
]
var myRoutes = []
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  if (to.path=="/login") {
    document.title = to.meta.title;
    next()
    return
  }
  var isAdminObj = JSON.parse(localStorage.vuex)
  if (!Cookies.get("tooken")&&to.meta.needtoken) {
    router.push('/login') // 先跳转
  }else{
      if ((!isAdminObj.isAdmin)&&to.meta.adminOnly) {
        router.push('/myArticle') // 先跳转
      }
      var navList = routes.find(item=>item.path=="/")['children'];
      var routerArr = [];
      for (let index = 0; index < navList.length; index++) {
        const el = navList[index];
        var obj={
          title:el.meta.title,
          name:el.name,
          path:el.path
        }
        if (el.path=="/readArticle") {
          continue;
        }
        if (isAdminObj.isAdmin) {
          routerArr.push(obj)
        }else{
          if (!el.meta.adminOnly&&el.meta.isNav) {
            routerArr.push(obj)
          }
        }
      }
      store.commit("setRouterList",routerArr);
    }
  console.log(to.meta.title);
  document.title = to.meta.title;
  next()
})
export default router
